<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 有效的正方形 -->
    <script>
      var validSquare = function (p1, p2, p3, p4) {
        const set = new Set([p1, p2, p3, p4].map(p => p[0] + ' ' + p[1]))
        if (set.size < 4) return false

        let [x1, y1] = p1
        let [x2, y2] = p2
        let [x3, y3] = p3
        let [x4, y4] = p4

        let l12 = Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)
        let l13 = Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2)
        let l14 = Math.pow(x4 - x1, 2) + Math.pow(y4 - y1, 2)

        let v12 = [x2 - x1, y2 - y1]
        let v13 = [x3 - x1, y3 - y1]
        let v14 = [x4 - x1, y4 - y1]

        if (l12 === l13 && l12 + l13 === l14) {
          let v23 = [x3 - x2, y3 - y2]
          return isVertical(v14, v23)
        } else if (l12 === l14 && l12 + l14 === l13) {
          let v24 = [x4 - x2, y4 - y2]
          return isVertical(v24, v13)
        } else if (l13 === l14 && l13 + l14 === l12) {
          let v34 = [x4 - x3, y4 - y3]
          return isVertical(v34, v12)
        } else {
          return false
        }
      }

      function isVertical(v1, v2) {
        return v1[0] * v2[0] + v1[1] * v2[1] === 0
      }
      console.log(validSquare((p1 = [0, 0]), (p2 = [2, 2]), (p3 = [2, 0]), (p4 = [0, 2])))
    </script>
  </body>
</html>
